package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.LmsSaveVideoTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

public class LmsSaveVideotmpppp {

    public static void main(String[] args) throws Exception {


        int sync = 1;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

    //    InputStream inputStream2 = new FileInputStream("libs//lms2022-x.user");
     //   InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//all.user");
           InputStream inputStream2 = new FileInputStream("libs//2022秋//fengx.user");
   //     InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//李俊杰2.user");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1];
            LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
            lmsWatchDTO.setUsername(userCode);
            lmsWatchDTO.setPassword(pwd);
//            if(!LmsUtil.getVip().contains(userCode)){
//                continue;
//            }
            if(!userCode.equals("2065001458390")){
                continue;
            }


            Map<Long, String> courseMap = Maps.newHashMap();
            Map<Long, String> allCourseMap =LmsUtil.getUserCourseMap(userCode);
            List<String> courses = Lists.newArrayList("中国近现代史纲要","习近平新时代中国特色社会主义思想","思想道德修养与法律基础","思想道德与法治","马克思主义基本原理","形势与政策");
         //   List<String> courses = Lists.newArrayList("思想道德与法治");


            for(Long kk:allCourseMap.keySet()){

                String c = allCourseMap.get(kk);
                if(courses.contains(c)){
                    courseMap.put(kk,c);
                }
            }

                  ;


//            courseMap.put(50000008099L,"毛泽东思想和中国特色社会主义理论体系概论");

            if(null == courseMap){
                continue;
            }
            Map<Long, String> newCourseMap = Maps.newHashMap();

            for(long id:courseMap.keySet()){
                String subjectName = courseMap.get(id);


                String completeInfoFilePath = "libs//2022秋//fengxLmsAnswerInfo//" + subjectName + "//";
                completeInfoFilePath = completeInfoFilePath + subjectName + "-study.txt";


                File completeInfoFile = new File(completeInfoFilePath);

                if(completeInfoFile.exists()){
                    InputStream completeInfoStream = new FileInputStream(completeInfoFile);
                    String completeInfoStr = IOUtils.toString(completeInfoStream, "UTF-8");
                    List<CompleteInfo> completeInfoList = JSONObject.parseArray(completeInfoStr, CompleteInfo.class);
                    if(completeInfoList!=null){
                        if(subjectName.equals("形势与政策")&&completeInfoList.size()>=5){
                            continue;
                        }
                        if(subjectName.equals("毛泽东思想和中国特色社会主义理论体系概论")&&completeInfoList.size()>=8){
                            continue;
                        }
                        if(subjectName.equals("中国近现代史纲要")&&completeInfoList.size()>=7){
                            continue;
                        }
                        if(subjectName.equals("思想道德与法治")&&completeInfoList.size()>=8){

                            System.err.println("??????????????????????");
                            continue;
                        }
                    }

                }

                newCourseMap.put(id,subjectName);
//                if(
////                        subjectName.equals("习近平新时代中国特色社会主义思想")
////                        || subjectName.equals("思想道德修养与法律基础")
////                        || subjectName.equals("形势与政策")
////                        || subjectName.equals("思想道德与法治")
////                        || subjectName.equals("毛泽东思想和中国特色社会主义理论体系概论")
////                        || subjectName.equals("中国近现代史纲要")
////                        || subjectName.equals("马克思主义基本原理概论")
//                        subjectName.equals("思想道德与法治")
//                ){
//
//                    newCourseMap.put(id,subjectName);
//                }
            }



//            if(!userCode.equals("2265001407382")){
//                continue;
//            }

            lmsWatchDTO.setCourseMap(newCourseMap);
            watchQueue.add(lmsWatchDTO);
        }

        Thread.sleep(2000);

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new LmsSaveVideoTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }


    }



}
